package com.sdgakj.foton.cloud.dictionary.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.sdgakj.foton.cloud.common.code.ResponseVO;
import com.sdgakj.foton.cloud.dictionary.base.bean.ProductType;
import com.sdgakj.foton.cloud.dictionary.code.ProductTypeResponseCode;
import com.sdgakj.foton.cloud.dictionary.service.IProductTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 拍卖Controller
 * @author wsp
 * @version 2018-04-25
 */
@Slf4j
@RestController
@RequestMapping(value = "/productType")
public class ApiProductTypeController {

	@Autowired
	private IProductTypeService productTypeService;

	/**
	 * 根据id查询对应的数据信息
	 * @param id 唯一标识
	 * @return id对应的数据信息
	 */
	@RequestMapping(path = "/get", method = RequestMethod.GET)
	public ResponseVO get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			ProductType entity = productTypeService.selectById(id);
			
			return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_RETURN_CODE_SUCCESS, entity);
		}
		return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_CODE_REQ_CANNOT_EMPTY, new ProductType());
	}
	
	/**
	 * 多维度信息分页查询
	 * @param productType 前台封装的参数对象
	 * @return ResponseVO结果
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public ResponseVO list(@RequestBody ProductType productType) {

		EntityWrapper<ProductType> entityWrapper = new EntityWrapper<ProductType>();
		entityWrapper.setEntity(productType);

		Page<ProductType> page = productTypeService.selectPage(new Page<ProductType>(productType.getCurrent(), productType.getSize()), entityWrapper);
		return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_RETURN_CODE_SUCCESS, page);
	}
	
	/**
	 * 添加数据
	 * @param productType 前台封装的参数对象
	 * @return ResponseVO结果
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ResponseVO insert(@RequestBody ProductType productType) {
		boolean result = productTypeService.insert(productType);
		if(result){
			return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_RETURN_CODE_SUCCESS, productType.getId());
		}
		return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_CODE_FAILURE, productType.getId());
	}
	
	/**
	 * 根据id更新数据
	 * @param productType 前台封装的参数对象
	 * @return ResponseVO结果
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseVO updateById(@RequestBody ProductType productType) {
		boolean result = productTypeService.updateById(productType);
		if(result){
			return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_RETURN_CODE_SUCCESS, productType.getId());
		}
		return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_CODE_FAILURE, productType.getId());
	}
	
	/**
	 * 根据id删除数据
	 * @param id 数据id
	 * @return ResponseVO结果
	 */
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public ResponseVO deleteById(@PathVariable(value = "id") String id) {
		boolean result = productTypeService.deleteById(id);
		if(result){
			return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_RETURN_CODE_SUCCESS, id);
		}
		return ProductTypeResponseCode.buildEnumResponseVO(ProductTypeResponseCode.RESPONSE_CODE_FAILURE, id);
	}


	@RequestMapping(value = "/getByName",method = RequestMethod.GET)
	public Object getByName(
			@RequestParam(value = "name",defaultValue = "") String name
	){
		return productTypeService.getByName(name);
	}
}